package sword.T58;

/**
 * @Author: 18362
 * @Create: 2022-04-10 13:34:42 星期日
 */
class Solution {
    public String reverseWords(String s) {
        String ss = s.trim();
        StringBuilder sb = new StringBuilder();
        int left = ss.length()-1, right = ss.length()-1;
        while (true) {
            while (left >= 0 && ss.charAt(left) != ' ')
                left--;
            for (int i=left+1; i<=right; ++i) {
                sb.append(ss.charAt(i));
            }
            while (left >= 0 && ss.charAt(left) == ' ')
                left--;
            right = left;
            if (left >= 0)
                sb.append(' ');
            else
                break;
        }
        return sb.toString();
    }
}
